package com.mvc4g.example.shareModel.client.view.templates;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.mvc4g.client.Controller;
import com.mvc4g.client.Event;
import com.mvc4g.client.ViewInterface;
import com.mvc4g.example.shareModel.client.Constants;
import com.mvc4g.example.shareModel.client.view.views.AbstractView;

public class RootTemplate implements ViewInterface, Constants {

	private SimplePanel panelA = new SimplePanel();
	private SimplePanel panelB = new SimplePanel();
	private SimplePanel panelC = new SimplePanel();

	private Controller controller = null;

	public RootTemplate() {

		Grid grid = new Grid(2, 3);
		
		grid.setBorderWidth(3);

		Button showA = new Button("ShowA");
		showA.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(SHOW_A, null);
				controller.handleEvent(e);
			}

		});

		Button showB = new Button("ShowB");
		showB.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(SHOW_B, null);
				controller.handleEvent(e);
			}

		});

		Button showC = new Button("ShowC");
		showC.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(SHOW_C, null);
				controller.handleEvent(e);
			}

		});

		grid.setWidget(0, 0, showA);
		grid.setWidget(0, 1, showB);
		grid.setWidget(0, 2, showC);
		grid.setWidget(1, 0, panelA);
		grid.setWidget(1, 1, panelB);
		grid.setWidget(1, 2, panelC);
		
		Button restart = new Button("Restart");
		restart.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(RESTART, null);
				controller.handleEvent(e);
			}

		});

		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(grid);
		mainPanel
				.add(new Label(
						"Widget B won't be display as long as Widget A & C aren't displayed"));
		mainPanel.add(restart);

		RootPanel.get().add(mainPanel);

	}

	@Override
	public void setController(Controller controller) {
		this.controller = controller;
	}

	public void changeWidgetA(AbstractView newView) {
		panelA.clear();
		newView.setVisible(true);
		newView.init();
		panelA.add(newView);
	}
	
	public void changeWidgetB(AbstractView newView) {
		panelB.clear();
		newView.setVisible(true);
		newView.init();
		panelB.add(newView);
	}
	
	public void changeWidgetC(AbstractView newView) {
		panelC.clear();
		newView.setVisible(true);
		newView.init();
		panelC.add(newView);
	}
	
	public void restart(){
		panelA.clear();
		panelB.clear();
		panelC.clear();		
	}
	
	

}
